package com.stylefeng.guns.modular.wz.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class IDUtil {
	
	private static final String ABCabc123 = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz0123456789";
	private static final String ABCabc123_ = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz0123456789_";
	
	/**
	 * 随机长度文件名
	 * @return
	 */
	public static String randomFileName(int len){
		int index;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			index = (int) (Math.random()*(ABCabc123.length()));
			sb.append(ABCabc123.charAt(index));
		}
		return sb.toString();
	}
	
	/**
	 * 生成UUID
	 * @return
	 */
	public static String genUUID() {
		return UUID.randomUUID().toString();
	}
	
	/**
	 * 生成app编号
	 * @return
	 */
	public static String genAppNO() {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
		StringBuilder sb = new StringBuilder();
		sb.append("A");
		sb.append(f.format(new Date()));
		sb.append(random(10));
		return sb.toString();
	}

	/**
	 * 生成一串20位的订单号
	 * 
	 * @return
	 */
	public static String genOrderNO() {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder();
		sb.append("D");
		sb.append(f.format(new Date()));
		sb.append(random(5));
		return sb.toString();
	}
	
	/**
	 * 生成一串20位的退单号
	 * 
	 * @return
	 */
	public static String genReturnOrderNO() {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder();
		sb.append("R");
		sb.append(f.format(new Date()));
		sb.append(random(5));
		return sb.toString();
	}
	
	/**
	 * 生成编号，带19个数字
	 * @param prefix
	 * @return
	 */
	public static String genNO(String prefix) {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder();
		sb.append(prefix);
		sb.append(f.format(new Date()));
		sb.append(random(5));
		return sb.toString();
	}
	
	/**
	 * 生成一串20位的交易号
	 * @return
	 */
	public static String genTransaNo() {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder();
		sb.append("D");
		sb.append(f.format(new Date()));
		sb.append(random(5));
		return sb.toString();
	}

	private static StringBuffer[] digits = { new StringBuffer("0"),
			new StringBuffer("1"), new StringBuffer("2"),
			new StringBuffer("3"), new StringBuffer("4"),
			new StringBuffer("5"), new StringBuffer("6"),
			new StringBuffer("7"), new StringBuffer("8"), new StringBuffer("9") };

	/**
	 * 产生一位数字
	 * 
	 * @param isZeroOK
	 *            这一位是否为0
	 * @return StringBuffer
	 */
	private static StringBuffer randomDigit(boolean isZeroOK) {
		int index;
		if (isZeroOK) {
			index = (int) Math.floor(Math.random() * 10);
		} else {
			index = 1 + (int) Math.floor(Math.random() * 9);
		}
		return (digits[index]);
	}

	/**
	 * 产生指定位数的一串数字
	 * 
	 * @param numDigits
	 *            int
	 * @return BigInteger
	 */
	public static String random(int numDigits) {
		StringBuffer s = new StringBuffer("");
		for (int i = 0; i < numDigits; i++) {
			if (i == 0) {
				// First digit must be non-zero.
				s.append(randomDigit(false));
			} else {
				s.append(randomDigit(true));
			}
		}
		return s.toString();
	}

	/**
	 * 生成一串10位的商品编号
	 * @return
	 */
	public static String genProductCode() {
		StringBuilder sb = new StringBuilder();
		sb.append("P");
		sb.append(random(9));
		return sb.toString();
	}
	
	/**
	 * 生成金融账户的账号
	 * @return
	 */
	public static String genFinanceAccount(){
		StringBuilder sb = new StringBuilder();
		sb.append("f");
		sb.append(random(19));
		return sb.toString();
	}

	/**
	 * 生成用户编号
	 * @return
	 */
	public static String genUserCode() {
		SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
		StringBuilder sb = new StringBuilder();
		sb.append("u");
		sb.append(f.format(new Date()));
		sb.append(random(5));
		return sb.toString();
	}
	
	/**
	 * 生成密钥
	 * @param len
	 * @return
	 */
	public static String genSecretKey(int len){
		int index;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			index = (int) (Math.random()*(ABCabc123_.length()));
			sb.append(ABCabc123_.charAt(index));
		}
		return sb.toString();
	}
	
	/**
	 * 生成出入库单号
	 * @param len
	 * @return
	 */
	public static String genCode(String prefix, int len){
		StringBuilder sb = new StringBuilder();
		sb.append(prefix);
		if (len>14) {
			SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
			sb.append(f.format(new Date()));
			len -= 14;
		}
		sb.append(random(len));
		return sb.toString();
	}

	/**
	 * 生成支付流水号
	 * @param len
	 * @return
	 */
	public static String genOutTranseNo(int len){
		StringBuilder sb = new StringBuilder();
		if (len>14) {
			SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
			sb.append(f.format(new Date()));
			len -= 14;
		}
		sb.append(random(len));
		return sb.toString();
	}
}
